<%--
  - Author		:	이 영호(Youngho Lee)	
  --%>
<%@ page language="java" %>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ page pageEncoding="utf-8" %>
<%@ include file="/WEB-INF/pages/includes/taglibs.jspf" %>
<%@ include file="/WEB-INF/pages/includes/variables.jspf" %>
<%@ include file="/WEB-INF/pages/includes/doctype.jspf" %>
<html>
	<head>
<%@ include file="/WEB-INF/pages/includes/metatags.jspf" %>
		<title><fmt:message key="pages.title" /></title>
		<link rel="stylesheet" type="text/css" href="<c:url value="${dirStyles}/common.css" />" />

		<script type="text/javascript" src="<c:url value="${dirScripts}/jquery-1.7.2.min.js" />"></script>
		<script type="text/javascript" src="<c:url value="${dirScripts}/jquery-ui-1.8.21.custom.min.js" />"></script>
		<script type="text/javascript" src="<c:url value="${dirScripts}/utils.js" />"></script>
		<script type="text/javascript">
			$(function(){
			});
			
			function validation(){
				try{
					with(document.frm){
						if(eval(chkDupl())){
							alert("동일한 모델이 등록되어 있습니다.");
							return false;
						}
						for(var i=0;i<$(".value").length;i++){
							if(($(".value").eq(i).val()).replace(/\s/g, '') == ""){
								alert($(".value").eq(i).attr("title")+"은 필수 입력 항목입니다.");
								$(".value").eq(i).focus();
								return false;
							}
						}
						
					}
				}catch(err){
					alert(err);
					return false;	
				}
				
				if(! confirm("단말기를 <c:if test="${empty device}">등록</c:if><c:if test="${not empty device}">수정</c:if> 하시겠습니까?")){
					return false;
				}
				
				return true;
			}
			
			function goAction(idx){
				document.listFrm.idx.value=idx;
				document.listFrm.submit();
			}

			function goPage(page){
				document.searchFrm.page.value = page;
				document.searchFrm.submit();
			}
			
			function chkDupl(){
				var result = false;
				
				var idx =  $("#idx").val();
				var model =  $(".value").eq(1).val();
				if(model.replace(/\s/g, '') == ""){
					alert("모델명을 입력하세요.");
					document.frm.id.focus();	
				}else{
					$.ajax({
						type: "POST",
						url: "<c:url value="/backoffice/device/chkDupl.do" />",
						dataType: "text",
						async: false,
						data: {
							"idx" : idx
							, "model" : model
						},
						success: function(data){
							result = data;
						},
						beforeSend: function(){
						},
						error: function(jqXHR, textStatus, errorThrown){
							//alert(jqXHR.responseText); //에러 페이지 내용
							alert("오류가 발생하였습니다.\n관리자에게 문의 바랍니다.");
						},
						complete: function(){
						}
					 });
				}
				return result;
			}
		</script>
	</head>
	<body>
<%@ include file="/WEB-INF/pages/includes/header.jsp" %>
		<div class="bodyWrap">
<%@ include file="/WEB-INF/pages/includes/left.jsp" %>
			<div class="contWrap">
				<div class="contMenuTitle">
					<img src="<c:url value="${dirImages}/subtit_07_2.gif" />" title="단말기 관리" alt="단말기 관리">
				</div>
				<div class="secTitle">
					<img src="<c:url value="${dirImages}/icon.png" />" title="단말기 관리" alt="단말기 관리" > 단말기 관리
				</div>
				<form name="frm" method="post" action="<c:url value="/backoffice/device/submit.do" />" onsubmit="return confirm('선택 단말기를 삭제하시겠습니까?');">
					<input type="hidden" name="fAct" value="remove">
					<table class="listTbl linked">
						<thead>
							<tr>
								<th>번호</th>
								<th>제조사</th>
								<th>모델명</th>
								<th>통신사</th>
								<th class="lastTh">삭제</th>
							</tr>
						</thead>
						<c:forEach items="${deviceList}" var="device" varStatus="stat">
							<tbody>
								<tr>
									<td onclick="javascript:goAction(${device.idx});">${fn:length(deviceList) - stat.index}</td>
									<td onclick="javascript:goAction(${device.idx});">
										<c:out value="${fn:split(device.value, ',')[0]}" />
									</td>
									<td onclick="javascript:goAction(${device.idx});">
										<c:out value="${fn:split(device.value, ',')[1]}" />
									</td>
									<td onclick="javascript:goAction(${device.idx});">
										<c:out value="${fn:split(device.value, ',')[2]}" />
									</td>
									<td>
										<input type="checkbox" name="idx" value="${device.idx}" />
									</td>
								</tr>
							</tbody>
						</c:forEach>
						<c:if test="${fn:length(deviceList) eq 0}">
							<tfoot>
								<tr>
									<td colspan="5">단말기가 없습니다.</td>
								</tr>
							</tfoot>
						</c:if>
					</table>
					<div class="btnWrap rightBtnWrap">
						<input type="submit" class="btn btn_delete" alt="삭제" title="삭제" value="">
					</div>
				</form>
				<br />
				<br />
				
				<div class="secTitle">
					<img src="<c:url value="${dirImages}/icon.png" />" title="빠른등록" alt="빠른등록" > 빠른등록
				</div>
				<form name="frm" method="post" action="<c:url value="/backoffice/device/submit.do" />" onsubmit="return validation();">
					<input type="hidden" id="idx" value="${device.idx}" />
					<table class="listTbl">
						<colgroup>
							<col width="*" />
							<col width="*" />
							<col width="*" />
						</colgroup>
						<thead>
							<tr>
								<th>제조사</th>
								<th>모델명</th>
								<th class="lastTh">통신사</th>
							</tr>
						</thead>
						<tbody>
							<tr>
								<td>
									<select class="value" name="value" title="제조사">
										<c:forEach items="${makerList}" var="maker">
											<option value="${maker.value}" <c:if test="${fn:split(device.value, ',')[0] eq maker.value}"> selected="selected" </c:if>>${maker.value}</option>
										</c:forEach>
									</select>
								</td>
								<td>
									<input type="text" class="value" name="value" title="모델명" alt="모델명" maxlength="25" value="${fn:split(device.value, ',')[1]}" /> 
								</td>
								<td>
									<select class="value" name="value" title="통신사">
										<option value="SKT" <c:if test="${fn:split(device.value, ',')[2] eq 'SKT'}"> selected="selected" </c:if>>SKT</option>
										<option value="KT" <c:if test="${fn:split(device.value, ',')[2] eq 'KT'}"> selected="selected" </c:if>>KT</option>
										<option value="LGU+" <c:if test="${fn:split(device.value, ',')[2] eq 'LGU+'}"> selected="selected" </c:if>>LGU+</option>
										<option value="기타" <c:if test="${fn:split(device.value, ',')[2] eq '기타'}"> selected="selected" </c:if>>기타</option>
									</select> 
								</td>
							</tr>
						</tbody>
					</table>
					<div class="btnWrap rightBtnWrap">
						<c:choose>
							<c:when test="${not empty device}">
								<input type="hidden" name="idx" value="${device.idx}">
								<input type="hidden" name="fAct" value="modify">
								<input type="submit" class="btn btn_revision" alt="수정" title="수정" value="">
							</c:when>
							<c:otherwise>
								<input type="submit" class="btn btn_register" alt="등록" title="등록" value="">
							</c:otherwise>
						</c:choose>
					</div>
				</form>
			</div>
		</div>
		<form name="listFrm" method="post" action="<c:url value="/backoffice/device/form.do" />">
			<input type="hidden" name="idx" />
		</form>
<%@ include file="/WEB-INF/pages/includes/footer.jsp" %>
	</body>
</html> 